package com.sanrenzu.everybodytv.ui.column;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.sanrenzu.everybodytv.R;
import com.sanrenzu.everybodytv.ui.column.adapter.ColumItemGridViewAdapter;
import com.sanrenzu.everybodytv.bean.zxb.ColumItemBean;
import com.sanrenzu.everybodytv.ui.vertical.colum.ColumItemActivity;
import com.sanrenzu.everybodytv.widget.MyGirdView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by My on 2016/9/5.
 */
public class ColumFragment extends Fragment implements ColumContract.View {

    private ColumContract.Presenter presenter;

    @BindView(R.id.columfragment_gridview)
    MyGirdView columfragmentGridview;
    private ColumItemGridViewAdapter columItemGridViewAdapter;
    private Unbinder bind;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.columfragment_item, container, false);
        presenter = new Columpresenter(this);
        presenter.onGetColumnItemUrl();
        bind = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onGetColumItemBean(final List<ColumItemBean> columItemBean) {
        columItemGridViewAdapter = new ColumItemGridViewAdapter(getActivity(),columItemBean);
        columfragmentGridview.setAdapter(columItemGridViewAdapter);

        columfragmentGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(getActivity(),ColumItemActivity.class);
                intent.putExtra("name",columItemBean.get(position).getSlug());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onGetColumItemBeanFailed(String error) {
        Log.i("TAG", "----->>>Failed: "+error);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }
}
